<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.5.0/axios.js"></script>
</head>
<body>
<button>发送get请求</button>
<button>发送post请求</button>
</body>
<script type="text/javascript">
    const btns = document.getElementsByTagName("button");

    const AxiosGet = axios.create({
        baseURL:"http://localhost:3000",
        method:"GET",
        timeout:2000
    })

    const AxiosPost = axios.create({
        baseURL:"http://localhost:3000",
        timeout:3000,
        data:{
            title:"hahaha",
            author:"laji"
        }
    })

    btns[0].onclick = function (){
        AxiosGet.get("/posts/1").then(response=>{
            console.log(response);
        })
    }

    btns[1].onclick = function (){
        AxiosPost.post("/posts").then(response=>{
            console.log(response);
        })
    }
    //请求拦截器
    axios.interceptors.request.use(function (){
        console.log("请求拦截器，成功");
    },function (error){
        console.log("请求拦截器，报错")
        return Promise.reject(error);
    })

    //响应拦截器
    axios.interceptors.response.use(function (){
        console.log("响应拦截器，成功");
    },function (error){
        console.log("响应拦截器，报错")
        return Promise.reject(error);
    })

    AxiosGet.interceptors.request.use(function (){
        console.log("请求拦截器，成功");
    },function (error){
        console.log("请求拦截器，报错")
        return Promise.reject(error);
    })
</script>
</html>